﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Data.SqlClient;

namespace Perm.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CheckVote
    {
        string end = "", String = "", select = "", error = null;
        
        SqlConnection conMSSQL;
        SqlDataReader readerMSSQL;
        SqlCommand commandMSSQL;
        
        DateTime thisDate = DateTime.Now.ToUniversalTime().AddHours(6);

        [OperationContract]
        public string DoWork(string city, string IP)
        {            
            string connectionStringMSSQL = WebConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString;
            
            try {
                conMSSQL = new SqlConnection(connectionStringMSSQL);
                conMSSQL.Open();
            } catch { error += "Невозможно подключиться к базе данных."; }
            
            string stringDate = thisDate.ToString();

            switch (city)
            {
                case "perm":
                    {
                        String = "SELECT IP.IP, IP.DATA FROM IP WHERE IP.IP='" + IP + "'";                        
                        break;
                    }
                case "ekb":
                    {
                        String = "SELECT IP_EKB.IP, IP_EKB.DATA FROM IP_EKB WHERE IP_EKB.IP='" + IP + "'"; 
                        break;
                    }
                case "permkray":
                    {
                        String = "SELECT IP_PERMKRAY.IP, IP_PERMKRAY.DATA FROM IP_PERMKRAY WHERE IP_PERMKRAY.IP='" + IP + "'"; 
                        break;
                    }
            }              
            commandMSSQL = new SqlCommand(String, conMSSQL);

            if (commandMSSQL.ExecuteScalar() != null)
            {
                readerMSSQL = commandMSSQL.ExecuteReader();
                readerMSSQL.Read();
                DateTime oldDate = Convert.ToDateTime(readerMSSQL[1].ToString());
                TimeSpan resultDate = thisDate - oldDate;
                int resultSec = Convert.ToInt32(Math.Round(double.Parse(resultDate.TotalSeconds.ToString()), 0));

                if (resultSec >= 1 * 60 * 60 * 24)
                {                    
                    end = null;
                }
                else
                {
                    end = "Вы голосовали: " + oldDate.ToString() + ", повторное голосование возможно: " + oldDate.AddDays(1).ToString() + ". ";
                }
            }
            else
            {
                end = null;
            }

            if (error != null)
            {
                switch (city)
                {
                    case "perm":
                        {
                            String = "INSERT INTO ERRORS (DATA, ERROR) VALUES ('" + thisDate + "', '" + error + "')";
                            break;
                        }
                    case "ekb":
                        {
                            String = "INSERT INTO ERRORS_EKB (DATA, ERROR) VALUES ('" + thisDate + "', '" + error + "')";
                            break;
                        }
                    case "permkray":
                        {
                            String = "INSERT INTO ERRORS_PERMKRAY (DATA, ERROR) VALUES ('" + thisDate + "', '" + error + "')";
                            break;
                        }
                }
                try
                {
                    commandMSSQL = new SqlCommand(String, conMSSQL);
                    commandMSSQL.ExecuteNonQuery();
                }
                catch { }
            }
            conMSSQL.Close();
            return end;   
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
